#include "drive_interactive.h"
#include "drive_console.h"


u8 console_operation_inspect(char* str,char* operation,struct command_t *analysis)
{
  char *token;
  char *pos = str;
  
  analysis->target = NULL;
	*token = *strstr(str,operation);
  if(token)
  {
    while(token!=pos)
    {
     if(!(*pos==' '||*pos==9))return 1;
     pos++;
    }
    
    pos += strlen(operation);
    if(*pos==' '||*pos==9)
    {
      while(*pos)
      {
        if(*pos!=' '&&*pos!=9)
        {
          if(*(pos)=='\r'&&*(pos+1)=='\n')
          {
            analysis->target = NULL;
            return 0;
          }
          else
          {
            analysis->target = pos;
            return 0;
          }
        }
        pos++;
      }
      analysis->target = NULL;
    }
    else if(*pos=='\r'&&*(pos+1)=='\n')
    {
      analysis->target = NULL;
      return 0;
    }
    else return 1;
      
    
    
  }return 1;

}

void console_command_inspect(char* str,struct command_t *analysis)
{
  if(!console_operation_inspect(str,"help",analysis))analysis->operation = help;
  
  else if(!console_operation_inspect(str,"restart",analysis))analysis->operation = restart;
  
  else if(!console_operation_inspect(str,"shutdown",analysis))analysis->operation = shutdown;
  
  else if(!console_operation_inspect(str,"ls",analysis))analysis->operation = ls;
  
  else if(!console_operation_inspect(str,"cd",analysis))analysis->operation = cd;
  
  else if(!console_operation_inspect(str,"get",analysis))analysis->operation = get;
  
  else if(!console_operation_inspect(str,"close",analysis))analysis->operation = close;
  
  else if(!console_operation_inspect(str,"open",analysis))analysis->operation = open;
  
  else analysis->operation = undefined;

}


void drive_console_input(char* data)
{
  struct command_t analysis;
  console_command_inspect(data,&analysis);
  

  if(analysis.operation == help)
  {
    drive_interactive_help(&analysis);
    
  }
  else if(analysis.operation == restart)
  {
    
  }
  else if(analysis.operation == ls)
  {
    drive_interactive_ls(&analysis);
  }
  else if(analysis.operation == undefined)
  {
    drive_interactive_undefined(&analysis);
  }
  
}